package com.hyy.book2.model;


import com.hyy.book2.constants.Constants;
import lombok.Data;
import org.apache.tomcat.util.bcel.Const;

@Data
public class Result<T> {//用于服务器返回数据
    private Integer code;//状态码，200成功 -1未登录 -2程序出错
    private String errMsg;//错误信息
    private T data;//泛型可以是BookInfo

    public static <T> Result success(T data) {
        Result result = new Result();
        result.setCode(Constants.SUCCESS_CODE);
        result.setErrMsg("");//根据接口文档，如果正确返回空
        result.setData(data);
        return result;
    }

    public static <T> Result fail() {
        Result result = new Result();
        result.setCode(Constants.FAIL_CODE);
        result.setErrMsg("程序错误，请联系管理员");
        return result;
    }

    public static <T> Result fail(String errMsg) {
        Result result = new Result();
        result.setCode(Constants.FAIL_CODE);
        result.setErrMsg(errMsg);
        return result;
    }

    public static <T> Result unlogin(){
        Result result = new Result();
        result.setCode(Constants.UNLOGIN_CODE);
        result.setErrMsg("未登录");
        return result;
    }

    public static <T> Result invalidParameter(){
        Result result = new Result();
        result.setCode(Constants.FAIL_CODE);
        result.setErrMsg("前端参数不合法");
        return result;
    }

}
